#include <stdint.h>
#include <stdbool.h>

#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

#include "pfc.h"
#include "debug.h"

//
//  Local constants
//
#define		MAX_FILELEN		32
#define		FMT_BUFFLEN		128
#define		DSP_BUFFLEN		256

//
//  Local driver data
//
static TRACE_LEVEL	lvl_display_low	 = ALL;
static TRACE_LEVEL	lvl_display_high = CRITICAL;

static char			t_fmt[FMT_BUFFLEN];
static char			t_str[DSP_BUFFLEN];

//------------------------------------------------------------------------------
//! _TR: trace function
//!
//! For debug builds (BLD_DEBUG), displays file, line, and string info.
//! For release builds it does nothing (via macro definition in debug.h).
//!
//! @param lvl_trace	- Message trace level
//! @param File			- Source file name
//! @param Line			- Source line number
//! @param ...			- Format string (printf style)
//! @return				- None
//------------------------------------------------------------------------------
void _TR(TRACE_LEVEL lvl_trace,	char* File, int Line, ...)
{
	va_list args;
	char* fmt;
	char* fi;

	if ((lvl_trace < lvl_display_low) || (lvl_trace	> lvl_display_high))		//	If trace is out of range
		return;																	//	Exit

	fi = strrchr(File, '/');
	fi = fi ? fi+1 : File;

	va_start(args, Line);
	fmt = (char *)va_arg(args, int);
	spf(t_fmt, "%s L: %4d: %s\n", fi, Line, fmt);								//	Write the base string + format
	spfv(t_str, t_fmt, args);													//	Add the rest to the line
	pf(t_str);

	if (lvl_trace == CRITICAL) exit(0);											//	If this is a critical message, exit
}

//
//	End: debug.c
//
